﻿using Common.Logging;
using Microsoft.Practices.Unity.InterceptionExtension;

namespace Easy.Common.Exceptions
{
    class ExceptionCallHandler : ICallHandler
    {
        //DEBUG < INFO < WARN < ERROR < FATAL
        private static readonly ILog domainLog = LogManager.GetLogger("DomainException");
        private static readonly ILog unknownLog = LogManager.GetLogger("UnknownException");


        public IMethodReturn Invoke(IMethodInvocation input, GetNextHandlerDelegate getNext)
        {
            var methodReturn = getNext().Invoke(input, getNext);

            if (methodReturn.Exception == null)
            {
                return methodReturn;
            }

            bool isDomainException = (methodReturn.Exception as DomainException) != null;

            //领域异常
            if (isDomainException)
            {
                domainLog.Info(methodReturn.Exception.Message, methodReturn.Exception);
            }
            else
            {
                unknownLog.Error(methodReturn.Exception.Message, methodReturn.Exception);
            }

            return methodReturn;
        }

        public int Order { get; set; }
    }
}
